import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;


/**
 * Service support echo input to output
 * @author hui.wang
 *
 */
public class Service extends Thread {
	private Socket _socket;
	
	public Service(Socket socket) {
		_socket = socket;
	}
	
	public void run(){
		try{
			//Wrapper the InputStream to BufferedReader
			BufferedReader input = new BufferedReader(new InputStreamReader(_socket.getInputStream()));
			//Wrapper the OutputStream to BuffedWriter
			BufferedWriter output = new BufferedWriter(new OutputStreamWriter(_socket.getOutputStream()));
			String line = input.readLine();
			while( line != null ) {
				if(line.equals("quit"))
					return;
				output.write(line + "\r\n");
				output.flush();
				line = input.readLine();
			}
			input.close();
			output.close();
		} catch(IOException e){
			//Abandon the current connection
		} finally{
			try{
				//Close the connection.
				_socket.close();
			} catch(IOException e){
				//Eat the IOException
			}
		}
	}
	

}
